package com.example.myapplicationwork;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ProfileEditActivity extends AppCompatActivity {
    private static final String TAG = "ProfileEditActivity";
    private static final int REQUEST_INTEREST = 1001;
    private String username;
    private TextView tvUsername;
    private TextView tvInterest;
    private Button btnBack;
    private EditText etOldPassword;
    private EditText etNewPassword;
    private Button btnChangePassword;
    private TextView tvPasswordHistory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_profile_edit);

        // 获取用户名
        Intent intent = getIntent();
        if (intent != null) {
            username = intent.getStringExtra("username");
        }

        // 初始化视图
        initViews();

        // 显示用户名
        tvUsername.setText("用户名: " + username);

        // 显示密码修改历史
        showPasswordHistory();

        // 返回按钮点击事件
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 兴趣选择文本框点击事件
        tvInterest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProfileEditActivity.this, InterestSelectActivity.class);
                startActivityForResult(intent, REQUEST_INTEREST);
            }
        });

        // 修改密码按钮点击事件
        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    // 初始化视图
    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        tvUsername = findViewById(R.id.tv_username);
        tvInterest = findViewById(R.id.tv_interest);
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        btnChangePassword = findViewById(R.id.btn_change_password);
        tvPasswordHistory = findViewById(R.id.tv_password_history);
    }

    // 修改密码逻辑
    private void changePassword() {
        String oldPassword = etOldPassword.getText().toString().trim();
        String newPassword = etNewPassword.getText().toString().trim();

        if (oldPassword.isEmpty() || newPassword.isEmpty()) {
            Toast.makeText(this, "旧密码和新密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 获取当前密码
        SharedPreferences sp = getSharedPreferences("password_record", MODE_PRIVATE);
        String currentPassword = sp.getString("current_password", "123456");

        if (!currentPassword.equals(oldPassword)) {
            Toast.makeText(this, "旧密码输入错误", Toast.LENGTH_SHORT).show();
            return;
        }

        if (oldPassword.equals(newPassword)) {
            Toast.makeText(this, "新密码不能与旧密码相同", Toast.LENGTH_SHORT).show();
            return;
        }

        // 保存新密码和修改记录
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("current_password", newPassword);

        // 记录修改时间和新密码
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).format(new Date());
        String history = sp.getString("password_history", "") + newPassword + "(" + time + ");";
        editor.putString("password_history", history);

        editor.apply();

        // 显示修改成功提示并更新历史记录
        Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
        showPasswordHistory();

        // 清空输入框
        etOldPassword.setText("");
        etNewPassword.setText("");
    }

    // 显示密码修改历史
    private void showPasswordHistory() {
        SharedPreferences sp = getSharedPreferences("password_record", MODE_PRIVATE);
        String history = sp.getString("password_history", "无修改记录");
        tvPasswordHistory.setText(history);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_INTEREST && resultCode == RESULT_OK && data != null) {
            String interests = data.getStringExtra("interests");
            if (interests != null && !interests.isEmpty()) {
                tvInterest.setText("兴趣: " + interests);
            }
        }
    }

    // 生命周期方法
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}
